import moment from 'moment'
import { toArrayBuffer } from '../str-format.js'
import { CRC16_1 } from '@/utils/crc'

/* 格式化为ArrayBuffer对象
 * @param {String} params 需要格式化的参数
 * @param {Object} buffer 最终的保存值
 * @param {Number} L2KeyOffset 偏移量
 * @param {Number} step 步长(数字：1， 文本：2)
 */
function setBuffer(param, buffer, L2KeyOffset, step) {
	if (!param) {
		buffer.L2Data[L2KeyOffset + 0] = 0
		L2KeyOffset += 1
	} else {
		buffer.L2Data[L2KeyOffset + 0] = param.length * step
		const bufferParam = toArrayBuffer(param, step)
		const viewParam = new Uint8Array(bufferParam)
		buffer.L2Data.set(viewParam, L2KeyOffset + 1)
		L2KeyOffset += param.length * step + 1
	}

	return {
		buffer,
		L2KeyOffset
	}
}

// 设置保存蓝牙数据
export default function(data) {
	let L2KeyOffset = 5
	let buffer = {
		L1Head: new Uint8Array(8),
		L2Data: new Uint8Array(200)
		
	}
	// mainstate
	buffer.L2Data[7] += 0x80

	// 语言设置
	if (data.countryIndex) {
		buffer.L2Data[7] += 0x01
	}
	// 软件停止
	if (data.appstop) {
		buffer.L2Data[7] += 0x04
	}

	// 启动方式
	if (data.startMode == '2') {
		buffer.L2Data[7] += 0x08
	}

	// 温度单位 0：℃ 1：℉
	if (data.unitIndex) {
		buffer.L2Data[7] += 0x10
	}

	L2KeyOffset += 3
	// 温度设置
	let buffertemplimit = new ArrayBuffer(2)
	let view121 = new DataView(buffertemplimit)
	// 温度上限
	view121.setInt16(0, data.maxtempIndex)
	buffer.L2Data[L2KeyOffset] = view121.getUint8(0)
	buffer.L2Data[L2KeyOffset + 1] = view121.getUint8(1)
	// 温度下限
	view121.setInt16(0, data.mintempIndex)
	buffer.L2Data[L2KeyOffset + 2] = view121.getUint8(0)
	buffer.L2Data[L2KeyOffset + 3] = view121.getUint8(1)
	L2KeyOffset += 4

	// 记录间隔
	buffer.L2Data[L2KeyOffset] = (data.recInterval & 0xFF00) >> 8
	buffer.L2Data[L2KeyOffset + 1] = data.recInterval & 0x00FF
	L2KeyOffset += 2

	// 延时时间
	buffer.L2Data[L2KeyOffset] = (data.recDelay & 0xFF00) >> 8
	buffer.L2Data[L2KeyOffset + 1] = data.recDelay & 0x00FF
	L2KeyOffset += 2

	//时区
	let timezone = (0 - new Date().getTimezoneOffset() / 60)
	buffer.L2Data[L2KeyOffset] = timezone
	buffer.L2Data[L2KeyOffset + 1] = 0
	buffer.L2Data[L2KeyOffset + 2] = 0
	buffer.L2Data[L2KeyOffset + 3] = 0
	L2KeyOffset += 4

	// 当前时间 YY/MM/DD H:m:ss	
	const format = ['YY', 'MM', 'DD', 'H', 'm', 'ss']
	format.forEach((item, index) => {
		buffer.L2Data[L2KeyOffset + index] = moment().format(item)
	})
	L2KeyOffset += 6

	// 查询码
	// 无查询码或查询码不等于6位则设置为空，即不设置查询码
	const id = data.customCheckIDInput
	const checkID = (id && id.length == 6) ? id : ''
	if (checkID) {
		buffer.L2Data[7] += 0x40
		buffer.L2Data[L2KeyOffset + 0] = checkID.length
		const bufferCheckID = toArrayBuffer(checkID, 1)
		const viewCheckID = new Uint8Array(bufferCheckID)
		buffer.L2Data.set(viewCheckID, L2KeyOffset + 1)
		L2KeyOffset += checkID.length + 1
	} else {
		// 没有设置查询码的设置7位0
		const emptyDate = Array.from({
			length: 7
		}, () => 0)
		emptyDate.forEach((item, index) => {
			buffer.L2Data[L2KeyOffset + index] = item
		})
		L2KeyOffset += 7
	}

	// 客户ID
	const bufferCustomName = setBuffer(data.customName, buffer, L2KeyOffset, 2)
	buffer = bufferCustomName.buffer
	L2KeyOffset = bufferCustomName.L2KeyOffset
	// 箱号	
	const bufferTrackID = setBuffer(data.trackID, buffer, L2KeyOffset, 1)
	buffer = bufferTrackID.buffer
	L2KeyOffset = bufferTrackID.L2KeyOffset
	// 备注
	const bufferNoteMark = setBuffer(data.noteMark, buffer, L2KeyOffset, 2)
	buffer = bufferNoteMark.buffer
	L2KeyOffset = bufferNoteMark.L2KeyOffset

	buffer.L2Len = L2KeyOffset
	buffer.L2Data[0] = 0x02 //CMD 配置
	buffer.L2Data[1] = 0x00 //version
	buffer.L2Data[2] = 0x01 //key 配置全部信息
	buffer.L2Data[3] = 0x00 //key len,MSB
	buffer.L2Data[4] = L2KeyOffset - 5 //key len ,LSB

	buffer.L1Head[0] = 0xCF //magic
	buffer.L1Head[1] = 0x00 //
	buffer.L1Head[2] = 0x00 //payload MSB
	buffer.L1Head[3] = buffer.L2Len //payload LSB
	const crcValue = CRC16_1(buffer.L2Data, buffer.L2Len)
	buffer.L1Head[4] = ((crcValue & 0xFF00) >> 8)
	buffer.L1Head[5] = ((crcValue & 0x00FF))
	buffer.L1Head[6] = 0x00 // key len ,LSB
	buffer.L1Head[7] = 0x11 // key len ,LSB

	return buffer
}
